package com.bootdo.fx.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.*;
import com.bootdo.fx.domain.UserInfoDO;
import com.bootdo.fx.service.UserInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.fx.domain.UserFundsInfoDO;
import com.bootdo.fx.service.UserFundsInfoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * 
 * @author 袁飞龙
 * @email 1992lcg@163.com
 * @date 2017-11-30 17:12:20
 */
 
@Controller
public class UserFundsInfoController {
	@Autowired
	private UserFundsInfoService userFundsInfoService;

	@Autowired
	private UserInfoService userInfoService;
	
	@GetMapping("/fx/userFundsInfo")
	@RequiresPermissions("fx:userFundsInfo:userFundsInfo")
	String UserFundsInfo(){
	    return "fx/userFundsInfo/userFundsInfo";
	}
	
	@ResponseBody
	@GetMapping("/fx/userFundsInfo/list")
	@RequiresPermissions("fx:userFundsInfo:userFundsInfo")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<UserFundsInfoDO> userFundsInfoList = userFundsInfoService.list(query);
		int total = userFundsInfoService.count(query);
		PageUtils pageUtils = new PageUtils(userFundsInfoList, total);
		return pageUtils;
	}
	
	@GetMapping("/fx/userFundsInfo/add")
	@RequiresPermissions("fx:userFundsInfo:add")
	String add(){
	    return "fx/userFundsInfo/add";
	}

	@GetMapping("/fx/userFundsInfo/edit/{fundsId}")
	@RequiresPermissions("fx:userFundsInfo:edit")
	String edit(@PathVariable("fundsId") Long fundsId,Model model){
		UserFundsInfoDO userFundsInfo = userFundsInfoService.get(fundsId);
		model.addAttribute("userFundsInfo", userFundsInfo);
	    return "fx/userFundsInfo/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/fx/userFundsInfo/save")
	@RequiresPermissions("fx:userFundsInfo:add")
	public R save( UserFundsInfoDO userFundsInfo){
		if(userFundsInfoService.save(userFundsInfo)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/fx/userFundsInfo/update")
	@RequiresPermissions("fx:userFundsInfo:edit")
	public R update( UserFundsInfoDO userFundsInfo){
		userFundsInfoService.update(userFundsInfo);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/userFundsInfo/remove")
	@ResponseBody
	@RequiresPermissions("fx:userFundsInfo:remove")
	public R remove( Long fundsId){
		if(userFundsInfoService.remove(fundsId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/userFundsInfo/batchRemove")
	@ResponseBody
	@RequiresPermissions("fx:userFundsInfo:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] fundsIds){
		userFundsInfoService.batchRemove(fundsIds);
		return R.ok();
	}


	/*
	* 获取分销管理的信息（包括个人佣金和可提现余额）
	* */
	@RequestMapping("/wx/userFundsInfo/getUserFundsInfoById")
	@ResponseBody
	public JsonResult getUserFundsInfoById(String userId,HttpServletRequest request, HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		if(userId == null || userId.length() == 0){
			return ResultUtil.renderError();
		}
		//UserInfoDO userInfo = userInfoService.get(Long.parseLong(userId));
		Map<String,Object> map = new HashMap<>();
		map.put("userId",userId);
		return ResultUtil.renderSuccess(userFundsInfoService.list(map));
	}


	/*
	* 获取我的团队信息（我的会员）
	* */
	@RequestMapping("/wx/userFundsInfo/getMyPersons")
	@ResponseBody
	public JsonResult getMyPersons(String userId,HttpServletRequest request, HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		//UserInfoDO userInfo = LoginUtils.getCurrentUser(request);
		Map<String,Object> map = new HashMap<>();
		if(userId == null && userId.length() == 0){
			ResultUtil.renderError();
		}
		map.put("upUserId",userId);
		List<Map<String,Object>> result = new ArrayList<>();
		List<UserInfoDO> list = userInfoService.list(map);
		if(list.size() == 0){
			return ResultUtil.renderSuccess("无推荐用户!");
		}
		map.clear();
		map.put("one",list);
		//获取一级会员
		result.add(map);

		String s1 = "";
		for(UserInfoDO u : list){
			s1 += u.getUserId() + ",";
		}
		List<UserInfoDO> list2 = userInfoService.listByUp(s1.substring(0,s1.length()-1));
		if(list2.size() == 0){
			return ResultUtil.renderSuccess(result);
		}
		Map<String,Object> map2 = new HashMap<>();
		//获取二级会员
		map2.put("two",list2);
		result.add(map2);

		String s2 = "";
		for(UserInfoDO u2 : list2){
			s2 += u2.getUserId() + ",";
		}
		List<UserInfoDO> list3 = userInfoService.listByUp(s2.substring(0,s2.length()-1));
		if(list3.size() == 0){
			return ResultUtil.renderSuccess(result);
		}
		Map<String,Object> map3 = new HashMap<>();
		//获取二级会员
		map3.put("three",list3);
		result.add(map3);

		return ResultUtil.renderSuccess(result);
	}
	
}
